//
//  SLRecordVC.swift
//  sulit-loan
//
//  Created by 王刚锋 on 2025/4/5.
//  Copyright © 2025 coolBoy. All rights reserved.
//

import UIKit

class SLRecordVC: SLBaseVC {

    private let leftVC = SLRecordListVC(id:"7", hiddenTab:false)
    private let centerVC = SLRecordListVC(id:"6", hiddenTab:false)
    private let rightVC = SLRecordListVC(id:"5", hiddenTab:false)
    var vcs : [SLBaseVC] = []
    var defaultSelectedIndex:Int = 0
    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationView.title = "Order records"
        self.navigationView.isHidden = false
        
        self.navigationView.backButon.isHidden = true
         
        addChildVC()
        
        
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if defaultSelectedIndex == 0 {
            self.leftVC.tableview.mj_header?.beginRefreshing()
            self.scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
        }else if defaultSelectedIndex == 1 {
            self.centerVC.tableview.mj_header?.beginRefreshing()
            self.scrollView.setContentOffset(CGPoint(x: DevConfig.width, y: 0), animated: false)
        }else if defaultSelectedIndex == 2 {
            self.rightVC.tableview.mj_header?.beginRefreshing()
            self.scrollView.setContentOffset(CGPoint(x: DevConfig.width*2, y: 0), animated: false)
        }
    }
    private func addChildVC(){
        self.addChild(leftVC)
        self.addChild(centerVC)
        self.addChild(rightVC)
        
        let v1 = UIView(frame: CGRectMake(0, 0, DevConfig.width, DevConfig.heigth-DevConfig.nav_height-50-DevConfig.tab_height))
        let v2 = UIView(frame: CGRectMake(DevConfig.width, 0, DevConfig.width, DevConfig.heigth-DevConfig.nav_height-50-DevConfig.tab_height))
        let v3 = UIView(frame: CGRectMake(DevConfig.width*2, 0, DevConfig.width, DevConfig.heigth-DevConfig.nav_height-50-DevConfig.tab_height))
        
        self.scrollView.addSubview(v1)
        self.scrollView.addSubview(v2)
        self.scrollView.addSubview(v3)
        
        v1.addSubview(leftVC.view)
        v2.addSubview(centerVC.view)
        v3.addSubview(rightVC.view)
        
        leftVC.view.snp.makeConstraints { make in
            make.top.bottom.left.right.equalTo(0)
        }
        centerVC.view.snp.makeConstraints { make in
            make.top.bottom.left.right.equalTo(0)
        }
        rightVC.view.snp.makeConstraints { make in
            make.top.bottom.left.right.equalTo(0)
        }
        
    }
    
    func setSelectIndex(index: Int){
        self.defaultSelectedIndex = index
        self.segmentedControl.setSelectedIndex(self.defaultSelectedIndex)
        self.scrollView.setContentOffset(CGPoint(x: Int(DevConfig.width)*index, y: 0), animated: true)
        if defaultSelectedIndex == 0 {
            self.leftVC.tableview.mj_header?.beginRefreshing()
        }else if defaultSelectedIndex == 1 {
            self.centerVC.tableview.mj_header?.beginRefreshing()
        }else if defaultSelectedIndex == 2 {
            self.rightVC.tableview.mj_header?.beginRefreshing()
        }
        
    }
    lazy var scrollView:UIScrollView = {
        let v = create_scrollView(supView: self.view) { make in
            make.left.right.bottom.equalTo(0)
            make.top.equalTo(segmentedControl.snp.bottom)
        }
        v.contentSize = CGSizeMake(DevConfig.width*3, DevConfig.heigth-DevConfig.nav_height-50-DevConfig.tab_height)
        v.isPagingEnabled = true
        v.delegate = self
        v.bounces = false
        return v
    }()
     lazy private var segmentedControl :SLRecordSegmentedControl = {
        let titles = ["In progress","Repayment","Finished"]
        let v = SLRecordSegmentedControl(titles: titles)
         v.setSelectedIndex(self.defaultSelectedIndex)
        v.onSegmentSelected = {[weak self] index in
            if let ss = self{
                ss.setSelectIndex(index: index)
            }
        }
        v.backgroundColor = .clear
         self.view.addSubview(v)
         v.snp.makeConstraints { make in
             make.left.equalTo(0)
             make.right.equalTo(0)
             make.height.equalTo(50)
             make.top.equalTo(self.navigationView.snp.bottom)
         }
        return v
    }()

}
extension SLRecordVC:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollView == self.scrollView{
            let page = self.scrollView.contentOffset.x/DevConfig.width
            self.segmentedControl.setSelectedIndex(Int(page))
            self.defaultSelectedIndex = Int(page)
        }
    }
}
